package com.zx.idc.backend.gui.common.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * http servlet 相关的工具类
 * 主要用于静态获取 request/response/session/sessionId
 *
 * @author rxliuli
 */
public final class HttpServletUtil {
    private HttpServletUtil() {
    }

    /**
     * 获取 request
     *
     * @return HttpServletRequest 对象
     */
    public static HttpServletRequest getRequest() {
        return ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
    }

    /**
     * 获取 response
     *
     * @return HttpServletResponse 对象
     */
    public static HttpServletResponse getResponse() {
        return ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getResponse();
    }

    /**
     * 获取 session
     *
     * @return HttpSession 对象
     */
    public static HttpSession getSession() {
        return getRequest().getSession();
    }

    /**
     * 获取会话标识 sessionId
     *
     * @return sessionId
     */
    public static String getSessionId() {
        return RequestContextHolder.currentRequestAttributes().getSessionId();
    }
}
